package corestruct

import (
	"core/action"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strings"
)

type HttpContext struct {
	Id      int64
	Session *HttpSession
	DB      *gorm.DB
	Http    *gin.Context
	LogName string         //操作对象的名称
	LogMemo string         //日志备注
	Pin     interface{}    //参数
	Pout    interface{}    //结果
	Act     *action.Action //当前Action
	Glid    string         //关联id
}

func (p HttpContext) GetIp() string {
	if nil == p.Http {
		return ""
	}
	return p.Http.RemoteIP()
}
func (p HttpContext) GetBrowser() string {
	if nil == p.Http {
		return ""
	}
	//User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
	var agent = p.Http.GetHeader("User-Agent")
	var r = ""
	if strings.Index(agent, "Chrome") > 0 {
		r = "Chrome"
	}
	return r
}
func (p HttpContext) GetOs() string {
	if nil == p.Http {
		return ""
	}
	//User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
	var agent = p.Http.GetHeader("User-Agent")
	var r = ""
	if strings.Index(agent, "Windows NT 10") > 0 {
		r = "WinNT10"
	}
	return r
}
func (p HttpContext) GetUrl() string {
	if nil == p.Http {
		return ""
	}
	var url = p.Http.Request.URL.Path
	if index := strings.Index(url, "?"); index > 0 {
		url = url[0:index]
	}
	url = strings.ToLower(url)
	return url
}

const KEY_HttpContext = "MyContext"
